package com.example.pest.service.impl.job;

import cn.hutool.core.date.DateUtil;
import com.example.pest.pojo.XxRoom;
import com.example.pest.pojo.XxRoomReservation;
import com.example.pest.service.impl.XxRoomReservationServiceImpl;
import com.example.pest.service.impl.XxRoomServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@AllArgsConstructor
public class JobRunner {

    private final XxRoomServiceImpl roomService;
    private final XxRoomReservationServiceImpl roomReservationService;
    public static final List<String> timeSpan = new ArrayList<String>(){{
        add("08:00~12:00");
        add("14:00~18:00");
        add("19:00~21:00");
    }};

    @Scheduled(initialDelay = 1000, fixedDelay = 2000)
    public void createRoomRese(){
       List<XxRoom> list = roomService.list();
       for (XxRoom room : list) {
           for (int i = 0; i < room.getPrefixDay(); i++) {
              String dk = DateUtil.format(DateUtil.offsetDay(new Date(),i),"yyyy-MM-dd");
              for (String sp :timeSpan) {
                  try {
                      XxRoomReservation reservation = new XxRoomReservation();
                      reservation.setRoomId(room.getId());
                      reservation.setCreateTime(System.currentTimeMillis());
                      reservation.setZwCount(room.getZwCount());
                      reservation.setDateKey(dk);
                      reservation.setUseCount(0);
                      reservation.setTimeSpan(sp);
                      roomReservationService.save(reservation);
                  } catch (Exception e) {
                  }
              }
           }
       }
    }
}
